﻿using Flurl.Http;
using Flurl;


namespace NineApi
{
    public static class FlurlExtensions
    {
        public static async Task<TResponse> GetJsonAsyncWithLowercaseParams<TResponse>(this IFlurlRequest request, object queryParam = null)
        {
            var formattedUrl = request.SetQueryParamsToLowercase(queryParam);
            return await formattedUrl.GetJsonAsync<TResponse>();
        }

        public static IFlurlRequest SetQueryParamsToLowercase(this IFlurlRequest request, object queryParam)
        {
            var queryParams = queryParam?.GetType().GetProperties()
                .ToDictionary(prop => prop.Name.ToLower(), prop => prop.GetValue(queryParam));
            return request.SetQueryParams(queryParams);
        }
    }
}
